<template>
<el-dialog title="添加角色" :visible.sync="visible">
  <el-container>
    <el-main>
      <el-form :model="addForm" ref="addForm" :rules="rules" size="mini" label-width="100px" :label-position="labelPosition">
        <el-form-item label="角色名称" prop="rolename">
          <el-input type="text" v-model="addForm.rolename" clearable placeholder="角色名称"/>
        </el-form-item>
        <el-form-item label="角色说明" prop="rolenote">
          <el-input type="textarea" v-model="addForm.rolenote" clearable placeholder="角色说明"/>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer>
      <el-button :loading="addBtn" type="primary" @click="submitForm('addForm')">确定</el-button>
      <el-button @click="resetForm('addForm')">重置</el-button>
    </el-footer>
  </el-container>
</el-dialog>
</template>

<script>
  import { addRoles } from '../../../api/system/functionRoles'
  import { getNum } from '../../../api/public/searchUnit'

  export default {
    name: 'functionRolesAdd',
    props: ['addVisible'],
    watch: {
      addVisible: function(val) {
        this.visible = val
      },
      visible: function(val) {
        this.$emit('update:addVisible', val)
      }
    },
    data() {
      return {
        visible: false,
        addBtn: false,
        labelPosition: 'right',
        addForm: {
          rolename: '',
          rolenote: ''
        },
        rules: {
          rolename: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm: function(addForm) {
        this.$refs[addForm].validate((valid) => {
          if (valid) {
            this.addBtn = true
            addRoles(this.addForm).then(res => {
              this.addBtn = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.visible = false
              this.$emit('randnum', getNum())
              this.$refs[addForm].resetFields()
            })
          } else {
            return false
          }
        })
      },
      resetForm: function(addForm) {
        this.addBtn = false
        this.$refs[addForm].resetFields()
      }
    }
  }
</script>

<style scoped>
.el-footer{
  text-align: center;
}
</style>
